package com.cskaoyan.connectionpool;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 创建日期: 2022/04/22 15:04
 *
 * @author ciggar
 *
 *
 * 创建自己的数据库连接池
 */
public class MyConnectionpool {

    // 连接池存放的容器  从头部存，从尾部取
    static LinkedList<Connection> connectionPool;

    static {

        // 初始化数据库连接池
        connectionPool = new LinkedList<>();

        // 往连接池里放Connection对象
        for (int i = 0; i < 10; i++) {
            Connection connection = JDBCUtils.getConnection();
            connectionPool.addFirst(connection);
        }
    }

    // 获取连接
    public static Connection getConnection(){

        Connection connection = connectionPool.removeLast();

        return connection;

    }

    // 返回连接
    public static void returnConnection(Connection connection){

        connectionPool.addFirst(connection);

    }
}
